const quickSort = (arr) => {
  if (arr.length <= 1) return arr;
  let flag = arr[0];
  let left = [];
  let right = [];
  for (let i = 1; i < arr.length; i++) {
    if (arr[i] <= flag) left.push(arr[i]);
    else right.push(arr[i]);
  }
  return [...quickSort(left), flag, ...quickSort(right)];
};

console.log(quickSort([3, 4, 1, 9, 4, 5, 16, 8, 7]));
 